﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RepairChecker
{
    public partial class stats : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RenderUI();
            }
        }

        void RenderUI()
        {
            List<District> districts = Entity.GetDistricts(false);
            lstDistrict.Items.Add(new ListItem("（全部）","0"));
            foreach (District d in districts)
            {
                lstDistrict.Items.Add(new ListItem(d.DistrictName, d.DistrictId.ToString()));
            }

            List<Fault> faults = Entity.GetAllFaults(true);
           // clstFault.Items.Add(new ListItem("（全部）", "0"));
            foreach (Fault f in faults)
            {
                clstFaults.Items.Add(new ListItem(f.FaultName, f.FaultId.ToString()));
            }

            lstJobType.Items.Add(new ListItem("（全部）", "0"));
            foreach (Entity.JobType j in Enum.GetValues(typeof(Entity.JobType)))
            {
                lstJobType.Items.Add(new ListItem(j.ToString(), ((int)j).ToString()));
            }

            List<Crew> crews = Entity.GetAllCrews();
            lstCrew.Items.Add(new ListItem("（全部）", "0"));
            foreach (Crew c in crews)
            {
                lstCrew.Items.Add(new ListItem(c.CrewName,c.CrewId.ToString()));
            }

            lstDateType.Items.Add(new ListItem("", "0"));
            lstDateType.Items.Add(new ListItem("接报时间", "1"));
            lstDateType.Items.Add(new ListItem("通知时间", "2"));
            lstDateType.Items.Add(new ListItem("到场时间", "3"));
            lstDateType.Items.Add(new ListItem("完成时间", "4"));

            caleStart.SelectedDate = DateTime.Now;
            caleEnd.SelectedDate = DateTime.Now;
        }

        protected void clstFaults_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtFaultString.Text = string.Empty;
            foreach (ListItem item in clstFaults.Items)
            {
                if (item.Selected == true) txtFaultString.Text += (txtFaultString.Text.Length == 0 ? "" : ",") + item.Value;
                
            }
            //dataFinishedJobs.DataBind();
        }

        protected void dataFinishedJobs_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                Job job = (Job)e.Row.DataItem;

                if (!String.IsNullOrEmpty(job.FaultIdString))
                {
                    BulletedList _lstFaults = (BulletedList)e.Row.FindControl("lstFaults");
                    List<Fault> faults = Entity.GetAllFaults(true);

                    foreach (string s in job.FaultIdString.Split(','))
                    {
                        if (!string.IsNullOrEmpty(s)) //
                        {
                            foreach (Fault f in faults)
                            {
                                if (f.FaultId == int.Parse(s))
                                {
                                    _lstFaults.Items.Add(f.FaultName);
                                    break;
                                }
                            }
                        }

                    }
                }
            }

        }

    }
}